package controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import model.TPhoto;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import annotation.Log;
import persistent.factory.DAOFactory;
import util.office.WriteExcel;
import util.toolkit.Constant;
import util.toolkit.ResponseJSON;

/**
 * 网站图片管理控制类
 * @author djf
 * @since 2018/12/13
 */

@Controller
@RequestMapping(value="/sysimgmanagement")
public class SysImgManagementController {
	
	/**
	 * 得到系统的所有图片
	 * @param model
	 * @param req
	 * @param resp
	 * @return
	 * @throws IOException
	 */
	//TODO 得到系统的所有图片
	@Log(operationType = Constant.OPERTATION_QUERY, operationDescribe = "得到系统的所有图片", 
			operationClassPath = "controller.SysTitleManagementController.getSystemImageList(Model model, HttpServletRequest req,HttpServletResponse resp)", 
			operationMenu = "网站图片管理/网站图片管理")
	@RequestMapping(value="/getsystemimagelist")
	public String getSystemImageList(Model model, HttpServletRequest req,HttpServletResponse resp) throws IOException{
		
		req.setCharacterEncoding("utf-8");
		resp.setCharacterEncoding("utf-8");
		resp.setContentType("application/json");

		JSONObject result = new JSONObject();
		
		try {
			List<TPhoto> list=DAOFactory.getPhotoDAO().getPhotoList();
			if(list!=null && list.size()>0){
				result.put("code", ResponseJSON.FLAG_SUCC);
				result.put("msg", "查询成功！");
				result.put("count", list.size());
				JSONArray jsonArray = JSONArray.fromObject(list);
				result.element("data", jsonArray);
			}else{
				result.put("code", ResponseJSON.FLAG_FAIL);
				result.put("msg", "查询失败！");
			}
			
		} catch (Exception e) {
			e.printStackTrace();
			result.put("code", ResponseJSON.FLAG_UNKNOWN_ERORR);
			result.put("msg", "查询失败！");
		}
		
		PrintWriter write = resp.getWriter();
		write.print(result);
		return null;
	}
	
	/**
	 * 得到系统图片(添加文章时使用)
	 * @param model
	 * @param req
	 * @param resp
	 * @return
	 * @throws IOException
	 */
	//TODO 得到系统图片(添加文章时使用)
	@Log(operationType = Constant.OPERTATION_QUERY, operationDescribe = "得到系统图片(添加文章时调用)", 
			operationClassPath = "controller.SysTitleManagementController.getInforImageList(Model model, HttpServletRequest req,HttpServletResponse resp)", 
			operationMenu = "网站信息管理/信息添加")
	@RequestMapping(value="/getinforimagelist")
	public String getInforImageList(Model model, HttpServletRequest req,HttpServletResponse resp) throws IOException{
		
		req.setCharacterEncoding("utf-8");
		resp.setCharacterEncoding("utf-8");
		resp.setContentType("application/json");

		JSONObject result = new JSONObject();
		
		try {
			List<TPhoto> list=DAOFactory.getPhotoDAO().getPhotoList();
			if(list!=null && list.size()>0){
				result.put("code", 0);
				result.put("msg", "查询成功！");
				result.put("count", list.size());
				JSONArray jsonArray = JSONArray.fromObject(list);
				result.element("data", jsonArray);
			}else{
				result.put("code", 1);
				result.put("msg", "查询失败！");
			}
			
		} catch (Exception e) {
			e.printStackTrace();
			result.put("code", 2);
			result.put("msg", "查询失败！");
		}
		
		PrintWriter write = resp.getWriter();
		write.print(result);
		return null;
	}
	
	
	
	/**
	 * 删除一张网站图片
	 * @param model
	 * @param req
	 * @param resp
	 * @return
	 * @throws IOException
	 */
	//TODO 删除一张网站图片
	@Log(operationType = Constant.OPERTATION_DELETE, operationDescribe = "删除一张网站图片", 
			operationClassPath = "controller.SysTitleManagementController.deleteSystemImage(Model model, HttpServletRequest req,HttpServletResponse resp)", 
			operationMenu = "网站图片管理/网站图片管理")
	@RequestMapping(value="/deleteimage")
	public String deleteSystemImage(Model model, HttpServletRequest req,HttpServletResponse resp) throws IOException{
		
		req.setCharacterEncoding("utf-8");
		resp.setCharacterEncoding("utf-8");
		resp.setContentType("application/json");

		JSONObject result = new JSONObject();
		WriteExcel writeexcel=new WriteExcel();
		
		try {
			String photoid=req.getParameter("photoid");
			if(photoid!=null && !photoid.equals("")){
				TPhoto photo=DAOFactory.getPhotoDAO().getAPhoto(Integer.parseInt(photoid));
				if(photo!=null){
					boolean row=DAOFactory.getPhotoDAO().deletePhoto(photo.getPhotoid());
					if(row){
						writeexcel.deleteAPhoto(photo.getPath());
						result.put("code", ResponseJSON.FLAG_SUCC);
						result.put("msg", "图片删除成功！");
					}else{
						result.put("code", ResponseJSON.FLAG_FAIL);
						result.put("msg", "图片删除失败！");
					}
				}else{
					result.put("code", ResponseJSON.FLAG_FAIL);
					result.put("msg", "图片删除失败！");
				}
			}else{
				result.put("code", ResponseJSON.FLAG_FAIL);
				result.put("msg", "请选择要删除的图片！");
			}
		} catch (Exception e) {
			e.printStackTrace();
			result.put("code", ResponseJSON.FLAG_FAIL);
			result.put("msg", "图片删除失败！");
		}
		
		PrintWriter write = resp.getWriter();
		write.print(result);
		return null;
	}

}
